package com.jml.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

//定义的一个序列化操作，可以序列化所有对象，一定要实现序列化接口RedisSerializer接口
public class MyRedisObjectSerializer implements RedisSerializer<Object> {

	private Converter<Object,byte[]> serializingConverter = new SerializingConverter();
	private Converter<byte[],Object> deserializingConverter = new DeserializingConverter();
	private static final byte[] EMPTY_BYTE_ARRAY=new byte[0];

	//序列化  对象转数组
	@Override
	public byte[] serialize(Object object) throws SerializationException {
		if(object==null){
			return EMPTY_BYTE_ARRAY;
		}
		return serializingConverter.convert(object);
	}
	//反序列化
	@Override
	public Object deserialize(byte[] bytes) throws SerializationException {
		if(bytes==null||bytes.length==0){
			return null;
		}
		return deserializingConverter.convert(bytes);
	}
}
